package cn.ztelovecly.springboot.framework.util.auth;

import cn.ztelovecly.springboot.framework.security.Passport;

import java.io.Serializable;
import java.util.List;
import java.util.Random;

/**
 * 用户帐号，未登录时则会为用户分配一个匿名帐号
 * @author june
 *
 */
public class LocalData implements Serializable {
    private static final long              serialVersionUID = -7501210429755782596L;
    /**
     * 当前访问用户的信息
     */
    private static final ThreadLocal<ExtUser> currencyUserHolder = new ThreadLocal<>();

    /**
     * 当前用户的护照信息
     */
    private static final ThreadLocal<Passport> passportHolder = new ThreadLocal<>();

    /**
     * 当前承租人所购买的应用列表
     */
    private static final ThreadLocal<List<String>> applicationHolder = new ThreadLocal<>();

    /**
     * 日志ID
     */
    private static final ThreadLocal<Long> logIdHolder = new ThreadLocal<>();

    /**
     * sessionId
     */
    private static final ThreadLocal<String> sessionIdHolder = new ThreadLocal<>();

    /**
     * 淘宝ATI
     */
    private static final ThreadLocal<String> atiHolder = new ThreadLocal<>();

    /**
     * IP
     */
    private static final ThreadLocal<String> ipHolder = new ThreadLocal<>();




    static Random random = new Random(System.currentTimeMillis());
    public static Long getLogId() {
        Long id = logIdHolder.get();
        if(id == null) {
            id = System.currentTimeMillis() * 10000 + random.nextInt(10000);
            setLogId(id);
        }
        return id;
    }

    public static void setLogId(Long id) {
        logIdHolder.set(id);
    }


    public static ExtUser getCurrentUser() {
        return currencyUserHolder.get();
    }

    public static void setCurrentUser(ExtUser page) {
        currencyUserHolder.set(page);
    }


    public static Passport getCurrentPassport() {
        return passportHolder.get();
    }

    public static void setCurrentPassport(Passport passport) {
        passportHolder.set(passport);
    }

    public static List<String> getCurrentApplicationCodeList() {
        return applicationHolder.get();
    }

    public static void setCurrentApplicationCodeList(List<String> codeList) {
        applicationHolder.set(codeList);
    }

    public static String getSessionIdHolder() {
        return sessionIdHolder.get();
    }

    public static void setSessionIdHolder(String sessionId) {
        sessionIdHolder.set(sessionId);
    }

    public static String getAti() {
        return atiHolder.get();
    }

    public static void setAti(String ati) {
        atiHolder.set(ati);
    }

    public static String getIp() {
        return ipHolder.get();
    }

    public static void setIp(String ip) {
        ipHolder.set(ip);
    }




    public LocalData() {
    }

}
